#include <algorithm>
#include <cstdio>
#include <cstring>

const int N = 2005;
int n, k;
int s1[N][N], s2[N][N], t1[N][N], t2[N][N];
char str[N][N];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("chess.in", "r", stdin);
  freopen("chess.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", str[i] + 1);
    for (int j = 1; j <= n; ++j) {
      s1[i][j] = s1[i][j - 1];
      s2[i][j] = s2[i - 1][j];
      if (str[i][j] == 'B') {
        s1[i][j]++;
        s2[i][j]++;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = k; j <= n; ++j) {
      if (s1[i][j - k] == 0 && s1[i][n] - s1[i][j] == 0 &&
          s1[i][j] - s1[i][j - k] != 0) {
        t1[i][j] = 1;
      }
    }
  }
  for (int i = k; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (s2[i - k][j] == 0 && s2[n][j] - s2[i][j] == 0 &&
          s2[i][j] - s2[i - k][j] != 0)
        t2[i][j] = 1;
    }
  }
  int ori = 0;
  for (int i = 1; i <= n; ++i) {
    if (s1[i][n] == 0) ++ori;
    if (s2[n][i] == 0) ++ori;
  }
  memset(s1, 0, sizeof(s1));
  memset(s2, 0, sizeof(s2));
  for (int i = 1; i <= n; ++i) {
    for (int j = k; j <= n; ++j) {
      s1[i][j] = s1[i - 1][j] + t1[i][j];
    }
  }
  for (int i = k; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      s2[i][j] = s2[i][j - 1] + t2[i][j];
    }
  }
  int ans = 0;
  for (int i = k; i <= n; ++i) {
    for (int j = k; j <= n; ++j) {
      ans = std::max(ans, s1[i][j] - s1[i - k][j] + s2[i][j] - s2[i][j - k]);
    }
  }
  printf("%d", ans + ori);
  return 0;
}